import { getWarrantyPolicyDetail, goodWarrantyPolicySave } from '@/api/goodsSet'
import _ from 'lodash'
import { ref } from 'vue'

export default () => {
  // 保修政策表单
  const warrantyPolicyForm = ref<GoodAPIRequest.SaveGoodWarrantyPolicy>({
    content: '',
    descr: '',
    goodsTypeVos: [],
    id: '',
    mobileShow: false,
    name: '',
    status: false,
  })

  // 保存保修政策表单加载
  const saveWarrantyPolicyLoading = ref(false)

  /**
   * 保存保修政策
   */
  const saveWarrantyPolicy = async () => {
    saveWarrantyPolicyLoading.value = true
    const res = await goodWarrantyPolicySave(warrantyPolicyForm.value)
    saveWarrantyPolicyLoading.value = false
    return res.data
  }

  // 获取保修政策详情加载
  const getWarrantyPolicyDetailLoading = ref(false)

  /**
   * 获取保修政策详情
   * @param {String} id - 保修政策id
   */
  const getwarrantyPolicyDetail = async (id: string) => {
    getWarrantyPolicyDetailLoading.value = true
    const res = await getWarrantyPolicyDetail(id)
    getWarrantyPolicyDetailLoading.value = false
    warrantyPolicyForm.value = {
      content: res.data.data.row.content,
      descr: res.data.data.row.descr,
      goodsTypeVos: _.cloneDeep(res.data.data.row.goodsTypeVos),
      id: res.data.data.row.id,
      mobileShow: res.data.data.row.mobileShow,
      name: res.data.data.row.name,
      status: res.data.data.row.status,
    }
  }

  return {
    warrantyPolicyForm,
    saveWarrantyPolicyLoading,
    getWarrantyPolicyDetailLoading,
    saveWarrantyPolicy,
    getwarrantyPolicyDetail,
  }
}
